package com.dearbinge.opentapi.content.im;

import com.baomidou.mybatisplus.plugins.Page;
import com.dearbinge.data.api.im.UserApplyService;
import com.dearbinge.data.pojo.im.ImUserApply;
import com.dearbinge.utils.ResultMsg;
import com.dearbinge.utils.enums.ResultStatusCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * 好友申请控制层
 *
 * @author wes
 * @date 2019/2/27
 */
@Api(tags = "5.2、通讯-用户申请")
@RestController
@RequestMapping("/userApply")
@CrossOrigin
@EnableAutoConfiguration
public class UserApplyController extends WebMvcConfigurerAdapter {

    @Autowired
    private UserApplyService userApplyService;

    /**
     * 根据ID删除
     */
    @ApiOperation(value = "根据ID删除", notes = "根据ID删除")
    @ApiImplicitParam(name = "id", value = "id", paramType = "query",required = true)
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public ResultMsg<String> delete(Integer id) {
        try {
            Integer dataCode = userApplyService.delete(id);
            if (dataCode > 0){
                return ResultMsg.ok("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }


    /**
     * 新增/修改
     */
    @ApiOperation(value = "新增/修改",notes = "新增/修改")
    @RequestMapping(value = "addOrUpdate",method = RequestMethod.POST)
    public ResultMsg<String> addOrUpdate(@RequestBody ImUserApply userApply){
        try {

            if (userApply.getId() != null && userApply.getId() != 0){
                Integer dataCode = userApplyService.update(userApply);
                if (dataCode.equals(5002) ) {
                    return ResultMsg.error(ResultStatusCodeEnum.CODE5002.getCode(), "friendId等于userId，不能自己添加自己");
                }
                if (dataCode == 1){
                    return ResultMsg.ok("修改成功");
                }
            }else{

                Integer dataCode = userApplyService.insert(userApply);
                if (dataCode.equals(5002) ) {
                    return ResultMsg.error(ResultStatusCodeEnum.CODE5002.getCode(), "friendId等于userId，不能自己添加自己");
                }
                if (dataCode == 1){
                    return ResultMsg.ok("新增成功");
                }

            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据ID查询
     */
    @ApiOperation(value = "根据ID查询",notes = "根据ID查询")
    @ApiImplicitParam(name = "id",value = "id",paramType = "query")
    @RequestMapping(value = "/getById",method = RequestMethod.GET)
    public ResultMsg<ImUserApply> getById(Integer id){
        try {
            ImUserApply userApply = userApplyService.getById(id);
            return ResultMsg.ok(userApply);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据用户ID查询好友申请列表
     */
    @ApiOperation(value = "根据ID查询全部",notes = "根据ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "friendId",value = "登录的用户id",paramType = "query",required = true),
            @ApiImplicitParam(name = "current", value = "当前页", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页面记录数", paramType = "query")})
    @RequestMapping(value = "/listByFriendId",method = RequestMethod.GET)
    public ResultMsg<Page> listByFriendId(Integer friendId,Integer current, Integer size){
        try {
            Page userApply = userApplyService.listByFriendId(friendId,current,size);
            return ResultMsg.ok(userApply);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }
}
